﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for FREWORK.xaml
    /// </summary>
    public partial class FREWORK : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        FREWORKData m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataRow dwABPOS;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        FREWORKReport m_rpt;
        // string OrderNumber="";
        
        #endregion
        public FREWORK()
        {
            InitializeComponent();
            this.Name = "返修单";
        }
        public override void SetUp (Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           
            
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new FREWORKData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }
        private void InitDataBinding()
        {
            this.setUI.Tables.Add("ABPOS");
            this.setUI.Tables.Add("WBBST");


            orlReadCommand = OracleDB.CreateCommand();

            #region "ABPOS"
            orlReadCommand.CommandText = "SELECT D.LSNAME1,B.ABPAUFKRS||'-'||TO_NUMBER(B.ABPAUFLFD)||'-'||TO_NUMBER(B.ABPAUFPOS) AS ORDERNO,"
                + " A.ABKKDAUFTNR,A.ABKKDZEICH,C.HRNNAME,B.ABPAUFKRS,B.ABPAUFLFD,B.ABPAUFPOS,B.ABPBESCHICH, B.ABPWAKZ, "
                + " TRIM(B.ABPART)||'-'||TRIM(B.ABPFARBE)||'-'||B.ABPVAR AS ARTICLE,B.ABPSOLLMENGE1/100 AS QTY,'' AS REASON "
                + " FROM ABKOPF A,ABPOS B,HRSTAMM C,LIEFDAT D "
                + " WHERE A.ABKMANDANT = B.ABPMANDANT AND A.ABKAUFKRS = B.ABPAUFKRS "
                + " AND A.ABKAUFLFD = B.ABPAUFLFD AND A.ABKMANDANT = C.HRMANDANT "
                + " AND A.ABKARTSB = C.HRBENID AND B.ABPMANDANT = D.LSMANDANT "
                + " AND B.ABPLIEFNR = D.LSLIEFNR AND A.ABKAUFART = 'AM'AND RowNum<1 ";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "ABPOS";
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion
          
            #region "WBBST"
            orlReadCommand.CommandText = "SELECT A.WBBSTKNR,ROUND(DECODE(A.WBBWAKZ,'F',A.WBBISTKLG/1000/0.9144,A.WBBISTKLG/1000),1) AS QTY,"
                    + " A.WBBDPART, A.WBBBEM,A.WBBAUFKRS,A.WBBAUFLFD,A.WBBAUFPOS "
                    + " FROM WBBST A WHERE RowNum<1 ";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "WBBST";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion


            setUI.AcceptChanges();
            this.ABPOSDataGrid.DataContext = setUI;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

  
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DataTable dtTemp in setUI.Tables)
            {
                if (dtTemp.TableName != "ABPOS")
                {
                    dtTemp.Clear();
                }
            }
            setUI.AcceptChanges();
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtORDER.Text = dwABPOS["ORDERNO"].ToString() ;
                //Tracer.Speak("销售单："+txtVKPAUFLFD.Text);
                ReloadWBBST(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }

        private void ReloadWBBST(DataRow dwABPOS)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwABPOS["ABPAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwABPOS["ABPAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwABPOS["ABPAUFPOS"];
            DataSet setResult = m_Data.GetWBBST(dwWBBST);
           
            setUI.Tables["WBBST"].Clear();
            setUI.Merge(setResult);
            listWBBST.DataContext = setUI;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                DataRow dwTmp;
                DataSet setRpt = new DataSet();
                string Customer = "";
                string Address = "";
                setRpt = m_Data.BuildData(dwWBBST);
                if (setRpt.Tables["WBBST"].Rows.Count > 0)
                {
                    dwTmp = setRpt.Tables["WBBST"].Rows[0];
                    dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                    dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                    dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                    m_DataX = new FREWORKData(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new FREWORKReport(dicParamater);
                    m_rpt.dsSource = setRpt;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetFREWORKReport("EXCEL");

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                    InitDataBinding();
                    setResult = m_Data.LoadABPOS();
                    setUI.Clear();
                    setUI.Merge(setResult);
                    this.DataContext = setUI;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "JustExcel";
            try
            {
                DataRow dwTmp;
                DataSet setRpt = new DataSet();
                string Customer = "";
                string Address = "";
                setRpt = m_Data.BuildData(dwWBBST);
                if (setRpt.Tables["WBBST"].Rows.Count > 0)
                {
                    dwTmp = setRpt.Tables["WBBST"].Rows[0];
                    dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                    dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                    dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                    m_DataX = new FREWORKData(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new FREWORKReport(dicParamater);
                    m_rpt.dsSource = setRpt;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetFBURLINGReport("EXCEL");

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQueryG_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "JustExcel";
            GGenerateReport(PrintType);
        }

        private void btnPrintPreviewG_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "JustExcel";
            try
            {
                DataRow dwTmp;
                DataSet setRpt = new DataSet();
                string Customer = "";
                string Address = "";
                setRpt = m_Data.BuildData(dwWBBST);
                if (setRpt.Tables["WBBST"].Rows.Count > 0)
                {
                    dwTmp = setRpt.Tables["WBBST"].Rows[0];
                    dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                    dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                    dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                    m_DataX = new FREWORKData(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new FREWORKReport(dicParamater);
                    m_rpt.dsSource = setRpt;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetGBURLINGReport("EXCEL");

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void GGenerateReport(string PrintType)
        {
            try
            {
                DataRow dwTmp;
                DataSet setRpt = new DataSet();
                string Customer = "";
                string Address = "";
                setRpt = m_Data.BuildData(dwWBBST);
                if (setRpt.Tables["WBBST"].Rows.Count > 0)
                {
                    dwTmp = setRpt.Tables["WBBST"].Rows[0];
                    dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                    dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                    dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                    m_DataX = new FREWORKData(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new FREWORKReport(dicParamater);
                    m_rpt.dsSource = setRpt;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetGREWORKReport("EXCEL");

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
